from django.db import models

# Create your models here.


class User(models.Model):
    phone = models.CharField(max_length=11, unique=True)
    name = models.CharField(max_length=20)
    pic = models.ImageField(null=True, blank=True, upload_to='avatar/', help_text='头像')
    class Meta:
        db_table = 'thirduser'
        verbose_name = "用户"
        verbose_name_plural = verbose_name

class ThirdUser(models.Model):
    TYPE_CHOICES = (
        (0, 'QQ'),
        (1, 'DingDing')
    )
    type = models.IntegerField(choices=TYPE_CHOICES, default=1)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    uid = models.CharField(max_length=100)
    token = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        db_table='thir_duser'
        verbose_name = "第三方登录"
        verbose_name_plural = verbose_name


# 用户身份
class IdCardUser(models.Model):
    name = models.CharField(max_length=20)
    code = models.CharField(max_length=50, unique=True,verbose_name="身份证号")
    sex = models.IntegerField(default=0, choices=((0, '男'), (1, '女')),verbose_name="性别")
    is_default = models.BooleanField(default=False, verbose_name="是否默认")
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='idcardusers',verbose_name="所属用户")

    class Meta:
        db_table = 'idcard_user'
        verbose_name = "身份信息"
        verbose_name_plural = verbose_name



# 分类表
class GoodsCategory(models.Model):
    title = models.CharField(max_length=30, verbose_name="分类名称")
    pid = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name="父级分类")

    class Meta:
        db_table = 'goods_category'
        verbose_name = "商品分类"
        verbose_name_plural = verbose_name
# 商品表
class Godds(models.Model):
    name = models.CharField(max_length=30, verbose_name="商品名称")
    pid = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name="分类")
    topid = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, related_name='goods', verbose_name="顶级分类")
    sale = models.IntegerField(default=0, verbose_name="销量")
    class Meta:
        db_table = 'godds'
        verbose_name = "商品"
        verbose_name_plural = verbose_name

# 用户浏览表
class UserBrowse(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    goods = models.ForeignKey(Godds, on_delete=models.CASCADE, verbose_name="商品")
    topid = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, related_name='browse', verbose_name="顶级分类")
    count = models.IntegerField(default=1, verbose_name="浏览次数")
    class Meta:
        db_table = 'userbrowse'
        verbose_name = "用户浏览记录"
        verbose_name_plural = verbose_name



class Foods(models.Model):
    name = models.CharField(max_length=30, verbose_name="食物名称")
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="价格")
    sale = models.IntegerField(default=0, verbose_name="销量")

    class Meta:
        db_table = 'foods'
        verbose_name = "食物"
        verbose_name_plural = verbose_name




# 服务器测试
class ServiceHost(models.Model):
    name = models.CharField(max_length=100, verbose_name="服务器名称")
    ip = models.GenericIPAddressField(unique=True, verbose_name="服务器IP")
    port = models.IntegerField(default=22, verbose_name="端口")
    username = models.CharField(max_length=30, verbose_name="账号")
    password = models.CharField(max_length=30, verbose_name="密码")
    threshold = models.IntegerField(default=90, verbose_name="阈值")
    email = models.EmailField(verbose_name="邮箱")
    class Meta:
        db_table = 'service_host'
        verbose_name = "服务器"
        verbose_name_plural = verbose_name
